﻿app.controller('MessageController',
['$scope',
'MessageService',
'$routeParams',
'$location',
'$window',
function ($scope, MessageService, $routeParams, $location, $window) {
    //var id = $routeParams.id;
    var id = $window.sessionStorage.getItem('messageId');

    if (id) {
        $scope.message = {};
        MessageService.getMessage(id, function (message) {
            $scope.message = message;
            $window.sessionStorage.removeItem('messageId');
        });
    }
    else {
        $scope.messages = [];
        MessageService.getMessages(function (messages) {
            for (var m in messages) {
                $scope.messages.push(messages[m]);
            }
        });
    }

    $scope.viewMessage = function (id) {
        $window.sessionStorage.setItem('messageId', id);
        $location.path('/message');
    };
}]);
